<template>
  <el-dialog v-drag :title="!dataForm.itemId ? '新增[' + dict.name + ']子项' : '修改[' + dict.name + ']子项'"
    :close-on-click-modal="false" :visible.sync="visible" width="30%" @close="dialogClosed">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="名称" prop="label">
        <el-input size="mini" v-model="dataForm.label" placeholder="字典项名称"></el-input>
      </el-form-item>
      <el-form-item label="项值" prop="value">
        <el-input size="mini" v-model="dataForm.value" placeholder="字典项值"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input size="mini" type="number" v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input size="mini" type="textarea" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          itemId: null,
          dictId: '',
          label: '',
          value: '',
          sort: '',
        },
        dict: {},
        dataRule: {
          label: [{
              required: true,
              message: '字典项名称不能为空',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 15,
              message: '字典项名称在2~15之间',
              trigger: 'blur'
            }
          ],
          value: [{
              required: true,
              message: '字典项值不能为空',
              trigger: 'blur'
            },
            {
              min: 1,
              max: 15,
              message: '字典项值在2~15之间',
              trigger: 'blur'
            },
          ],
        }
      }
    },
    methods: {
      init(dict) {
        this.dataForm.dictId = dict.itemId
        this.dict = dict
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      dialogClosed() {
        this.$refs['dataForm'].resetFields()
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate(async (valid) => {
          if (valid) {
            const res = await this.$http({
              url: `/base/basedictitem/${!this.dataForm.itemId ? 'save' : 'update'}`,
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            })
            const data = res.data
            if (data && data.code === 0) {
              this.$message({
                message: '添加成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          }
        })
      }
    }
  }
</script>
